package top100

func findAnagrams(s string, p string) []int {
	if len(s) < len(p) {
		return []int{}
	}

	var winArray = new([26]int)
	for ch := range p {
		winArray[p[ch]-'a']++
	}
	var curWinArray = new([26]int)
	for i := 0; i < len(p); i++ {
		curWinArray[s[i]-'a']++
	}
	var res []int
	if compare(*curWinArray, *winArray) {
		res = append(res, 0)
	}
	var left = 0
	for i := len(p); i < len(s); i++ {
		curWinArray[s[left]-'a']--
		curWinArray[s[i]-'a']++
		left++
		if compare(*curWinArray, *winArray) {
			res = append(res, left)
		}

	}
	return res
}

func compare(a, b [26]int) bool {
	for i := 0; i < 26; i++ {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}
